package com.base.vistter.iframe.inverter.controller;

import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.entity.SysProject;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.service.SysProjectService;
import com.base.vistter.iframe.utils.IoUtils;
import com.base.vistter.iframe.utils.UploadUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/sys/project")
public class SysProjectController {

    @Resource(name = "sysProjectService")
    private SysProjectService sysProjectService;
    @GetMapping()
    public Result<SysProject> get() throws IframeException {
        return Result.ok(sysProjectService.selectOne());
    }

    @PutMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> update(@RequestBody SysProject sysProject) throws IframeException {
        return Result.ok(sysProjectService.update(sysProject));
    }

    @GetMapping("/background")
    public void background(HttpServletResponse response) throws IframeException {
        SysProject sysProject = sysProjectService.selectOne();
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setContentType("image/" + UploadUtils.getFileExt(sysProject.getBackage()));
        IoUtils.fileToStream(sysProject.getBackage(), response);
    }

}
